package com.xiaoy.core.basic.security.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.xiaoy.core.basic.security.entity.Role;
import com.xiaoy.core.basic.security.service.RoleService;
import com.xiaoy.util.StringUtil;
import com.xiaoy.util.page.PageInfo;
import com.xiaoy.web.BasicAction;

@Namespace("/security")
@Results( {
	@Result(name = "error", location = "/WEB-INF/content/workspace/exception.jsp", type = "dispatcher"),
	
	@Result(name = "rolelist", location = "/bas/jsp/security/role-list.jsp", type = "dispatcher"),
	@Result(name = "addrole", location = "/bas/jsp/security/addrole.jsp", type = "dispatcher"),
	
	
	@Result(name = "saverole", location = "/common/jsp/success.jsp", type = "dispatcher"),
	
})
public class RoleAction extends BasicAction{
	
	@Resource(name="roleService")
	private RoleService roleService;
	
	
	public String roleList () {
		
		return "rolelist";
	}
	
	public String saveRole () {
		
		long pid = getParameterLong("pid");
		String name = getParameter("name");
		long sort = getParameterLong("sort");
		long priority = getParameterLong("priority");
		
		
//		System.out.println("pid-->"+pid);
		
		Role role = new Role();
		
		role.setName(name);
		role.setPriority(priority);
		role.setSort(sort);
		role.setCreatetime(new Date());
		role.setCreateuser(getLoginUser());
//		role.setType(type);
		
		roleService.save(role, pid);
		
		PageInfo page = new PageInfo();
		page.setAutoUrl("security/role!addRole.do");
		page.setAutoInfo("秒钟后，将返回角色添加页面！");
		page.setBackUrl("security/role!addRole.do");
		page.setBackInfo("继续添加");
		page.setMessage("保存角色(<b>"+name+"</b>)完成.");
		request.setAttribute("pageInfo", page);
		
		
		return "saverole";
	}
	
	public String addRole () {
		
//		String name = getParameter("name");
//		List<Role>roles = roleService.getRoleTree();
//		Role r = new Role();
//		r.setId(0);
//		r.setName("角色");
//		roles.add(r);
//		request.setAttribute("roles", roles);
//		System.out.println(name);
		return "addrole";
	}
	
	public String deleteRole () {
		
		
		long roleid = getParameterLong("roleid");
		Role role = roleService.delete(roleid);
		
		PageInfo page = new PageInfo();
		page.setAutoUrl("organization/organization!getAllType.do");
		page.setAutoInfo("秒钟后，将返回组织机构类型列表页面！");
		page.setBackUrl("organization/organization!getAllType.do");
		page.setBackInfo("返回列表页面");
		page.setMessage("删除组织结构类型(<b>"+role.getName()+"</b>)完成.");
		request.setAttribute("pageInfo", page);
		
		return "deleteRole";
	}
	
	public void getRoleTree () {
		
		List<Role>roles = roleService.getRoleTree();
		
		Role r = new Role();
		r.setId(0);
		r.setName("顶级");
		roles.add(r);
//		String json = JSONUtils.listToJson(roles);
		
		String json = rolesToJson(roles);
		
		System.err.println("json -- > "+json);
		Struts2Utils.renderJson(json);
	}
	
	private String rolesToJson (List<Role>roles) {
		
		StringBuilder sb = new StringBuilder("[ ");
		for (Role role:roles) {
			sb.append(roleToJson(role)).append(",");
		}
		sb.setCharAt(sb.length() - 1, ' ');
		sb.append("]");
		return sb.toString();
	}
	
	private String roleToJson (Role role){
		StringBuilder sb = new StringBuilder("{");
		
		sb
		.append(StringUtil.toJsonAttr("id", role.getId())).append(",")
		.append(StringUtil.toJsonAttr("text", role.getName())).append(",");
		
		if (role.getChildren()!=null&&role.getChildren().size()>0) {
			sb.append(StringUtil.toJsonAttrWithObj("children",rolesToJson(role.getChildren()))).append(",");
		}
		
		sb.append(StringUtil.toJsonAttr("priority", role.getPriority()));
		
		sb.append("}");
		return sb.toString();
	}
}
